<template>
  <div class="tab-pane fade" :id="id" role="tabpanel">
    <div class="card col-12">
      <div class="card-body">
        <form class="form-horizontal">
          <div class="row mb-1">
            <label class="control-label col-2 ms-auto text-end">企业ID</label>
            <div class="col-4 me-auto">
              <input type="input" class="form-control" v-model.trim="corpId" />
            </div>
          </div>
          <div class="row mb-1">
            <div class="col-4"></div>
            <div class="col-6">
              <div class="form-check form-check-inline">
                <input
                  class="form-check-input"
                  type="radio"
                  value="0"
                  id="msgType0"
                  v-model="msgType"
                />
                <label class="form-check-label">自建应用</label>
              </div>
              <div class="form-check form-check-inline">
                <input
                  class="form-check-input"
                  type="radio"
                  value="1"
                  id="msgType1"
                  v-model="msgType"
                />
                <label class="form-check-label">通讯录</label>
              </div>
            </div>
          </div>
          <div class="row">
            <label class="control-label col-2 ms-auto text-end">AgentId</label>
            <div class="col-4 me-auto">
              <input type="input" class="form-control" v-model.trim="agentId" />
            </div>
          </div>
          <div class="row">
            <label class="control-label col-2 ms-auto text-end">Secret</label>
            <div class="col-4 me-auto">
              <input
                type="input"
                class="form-control"
                v-model.trim="agentSecret"
              />
            </div>
          </div>
          <div class="row">
            <label class="control-label col-2 ms-auto text-end">令牌</label>
            <div class="col-4 me-auto">
              <input type="input" class="form-control" v-model.trim="token" />
              <span class="text-muted small"></span>
            </div>
          </div>
          <div class="row">
            <label class="control-label col-2 ms-auto text-end"
              >消息加解密密钥</label
            >
            <div class="col-4 me-auto">
              <input
                type="input"
                class="form-control"
                v-model.trim="encodingKey"
              />
              <span class="text-muted small"></span>
            </div>
          </div>
          <div class="row">
            <button
              id="btnChange"
              type="button"
              class="btn btn-primary col-4 offset-4"
              :disabled="saving"
              @click.stop="saveWorkWeixinConfig()"
            >
              保存
            </button>
          </div>
        </form>
      </div>
      <table class="table">
        <thead>
          <tr>
            <th>企业ID</th>
            <th>类型</th>
            <th>应用ID</th>
            <th>secret</th>
            <th>Token</th>
            <th>EncodingAESKey</th>
            <th>创建时间</th>
            <th>修改时间</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="info in dataList" :key="info.id">
            <td>{{ info.corpId }}</td>
            <td>{{ info.msgType }}</td>
            <td>{{ info.agentId }}</td>
            <td>{{ info.secret }}</td>
            <td>{{ info.token }}</td>
            <td>{{ info.encodingAESKey }}</td>
            <td>{{ info.createTime }}</td>
            <td>{{ info.lastUpdate }}</td>
            <td>
              <button type="button" @click.stop="edit(info)">修改</button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script>
import { searchWorkWeixinConfig, saveWorkWeixinConfig } from "@/api/admin.js";

export default {
  props: {
    id: {
      type: String,
    },
  },
  data() {
    return {
      dataList: [],
      enterpriseId: 0,
      corpId: "",
      msgType: 0,
      agentId: "",
      agentSecret: "",
      token: "",
      encodingKey: "",

      saving: false,
    };
  },
  mounted: function () {
    this.searchWorkWeixinConfig();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    searchWorkWeixinConfig: function () {
      searchWorkWeixinConfig((v) => {
        this.dataList = v;
      });
    },
    saveWorkWeixinConfig: function () {
      const params = {
        corpId: this.corpId,
        msgType: this.msgType,
        agentId: this.agentId,
        agentSecret: this.agentSecret,
        token: this.token,
        encodingKey: this.encodingKey,
      };

      this.saving = true;

      saveWorkWeixinConfig(
        params,
        (v) => {
          if (v.status === "OK") {
            this.showErrMsg("保存成功");
            this.searchWorkWeixinConfig();
          }
          else this.showErrMsg(v.errmsg, "danger");
        },
        () => {
          this.saving = false;
        }
      );
    },
    edit: function (v) {
      this.enterpriseId = v.enterpriseId;
      this.msgType = v.msgType;
      this.corpId = v.corpId;
      this.agentId = v.agentId;
      this.agentSecret = v.secret;
      this.token = v.token;
      this.encodingKey = v.encodingAESKey;
    },
  },
};
</script>